//
//  InvoiceStatisticsDetailVC.m
//  NtoccShipper
//
//  Created by mac on 2020/1/7.
//  Copyright © 2020 zhuchongjin. All rights reserved.
//

#import "InvoiceStatisticsDetailVC.h"
#import "InvoiceStatisticsDetailCell.h"
#import "InvoiceStatisticsDetailModel.h"

@interface InvoiceStatisticsDetailVC ()<UITableViewDelegate,UITableViewDataSource>{
    FlexFrameView *header;
}
@property (nonatomic,strong) InvoiceStatisticsDetailCell *invoiceStatisticsDetailCell;
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic,strong) NSMutableArray *modelArray;
@property (nonatomic,strong) NSString *refreshType; //刷新
@property (nonatomic,readwrite) int page;
@property (nonatomic,strong) InvoiceStatisticsDetailModel * model;
@property (nonatomic,strong) CJLabel *labInvoiceTitle;// -- 发票抬头;
@property (nonatomic,strong) CJLabel *labInvoiceNumber;// -- 发票号码
@property (nonatomic,strong) CJLabel *labFreightAmount;// -- 货主金额
@property (nonatomic,strong) CJLabel *labInvoiceAmount;// -- 开票金额
@end

@implementation InvoiceStatisticsDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"发票详情"];
    [self setBackLeftButton:@""];
    
    self.invoiceStatisticsDetailCell = [[InvoiceStatisticsDetailCell alloc]initWithFlex:nil reuseIdentifier:nil];
    [self.view addSubview:self.tableView];
    WS(ws);
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
           [ws.tableView.mj_header beginRefreshing];
    }];
    [self initData];
    [self loadData];
}

- (void)initData{
    self.modelArray = [[NSMutableArray alloc]init];
    self.page = 1;
}

- (void)loadData{
    
    NSString *strUrl = CJStringWithFormat(@"%@/ntocc-basic-api/invoice/simpleDetail",CJBaseUrl);
    NSDictionary *dicPra = @{
//        @"pageNum":CJStringWithFormat(@"%d",self.page),
//                             @"pageSize":@"10",
                             @"code":self.strCode //@"7da275a8cde840b8980107dbea7f90b4" //
    };
    WS(ws);
    [[HDNetworking sharedHDNetworking] GET:strUrl parameters:dicPra success:^(id  _Nonnull responseObject) {
        
        DLog(@"++++++++%@",responseObject);
        NSDictionary *dic = (NSDictionary *)responseObject;
        NSError * error ;
        self.model = [[InvoiceStatisticsDetailModel alloc]initWithDictionary:dic error:&error];

        NSArray *arr = [DetailListModel arrayOfModelsFromDictionaries:ws.model.detailList error:&error];
        [ws.modelArray removeAllObjects];
        [ws.modelArray addObjectsFromArray:arr];
        DLog(@"==== %@",ws.modelArray);

//        NSArray *list = responseObject[@"records"];
//        NSString *total = CJStringWithFormat(@"%@",responseObject[@"total"]);
//        if (list.count > 0) {
//
//           if ([ws.refreshType isEqualToString:@"down"]) {
//               [ws.modelArray removeAllObjects];
//           }
//
//           NSError * error ;
//           NSArray *arr = [InvoiceStatisticsDetailModel arrayOfModelsFromDictionaries:list error:&error];
//
//
//
//           [ws.modelArray addObjectsFromArray:arr];
//
//           [ws.tableView.mj_header endRefreshing];
//           [ws.tableView.mj_footer endRefreshing];
//
//           if (ws.modelArray.count == total.intValue) {
//               [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//           }
//
//        }else{
//           [ws.modelArray removeAllObjects];
//           [ws.tableView.mj_header endRefreshing];
//           [ws.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        [ws createTopView];

    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
//        [self.tableView.mj_header endRefreshing];
//        [self.tableView.mj_footer endRefreshingWithNoMoreData];
    }];

}

- (void)createTopView{
    //    WS(ws);
    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    header = [[FlexFrameView alloc] initWithFlex:@"InvoiceStatisticsDetailHeaderView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    //    @property (nonatomic,strong) CJLabel *labInvoiceTitle;// -- 发票抬头;
    //    @property (nonatomic,strong) CJLabel *labInvoiceNumber;// -- 发票号码
    //    @property (nonatomic,strong) CJLabel *labFreightAmount;// -- 货主金额
    //    @property (nonatomic,strong) CJLabel *labInvoiceAmount;// -- 开票金额
    InvoiceModel *invoiceModel = self.model.invoice;
    self.labInvoiceTitle.text = invoiceModel.invoiceTitle;
    self.labInvoiceNumber.text = [CJControl getIsEmpty:invoiceModel.invoiceNumber] ? @"":invoiceModel.invoiceNumber;
    self.labFreightAmount.text = CJStringWithFormat(@"￥%@",@(invoiceModel.freightAmount).cj_money_description);
    self.labInvoiceAmount.text = CJStringWithFormat(@"￥%@",@(invoiceModel.invoiceAmount).cj_money_description);

    [header layoutIfNeeded];
    self.tableView.tableHeaderView = header;
    [self.tableView reloadData];
}

- (void)downRefresh{
    self.refreshType = @"down";
    self.page = 1;
    [self loadData];
}

- (void)upRefresh{
    self.refreshType = @"up";
    self.page ++;
    [self loadData];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"InvoiceStatisticsDetailCell";
    InvoiceStatisticsDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[InvoiceStatisticsDetailCell alloc] initWithFlex:nil reuseIdentifier:identifier];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    DetailListModel *model = self.modelArray[indexPath.row];
    [cell setCellInfoWithModel:model];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
}

- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, kTopHeight + CJTableToTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - CJTableToTopHeight),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
//        .tbMjHeadFooter(YES)
        ;
//        WS(ws);
//        _tableView.blockMjHeader = ^(BOOL isheader) {
//
//            if (isheader) {
//                [ws downRefresh];
//            } else {
//                [ws upRefresh];
//            }
//
//        };
    }
    
    return _tableView;
}

@end
